#include "batchdlg.h"
#include "ui_batchdlg.h"

#include <QGraphicsOpacityEffect>
#include <QListView>

BatchDlg::BatchDlg(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::BatchDlg)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowModality(Qt::ApplicationModal);
    this->setModal(true);
    this->setAttribute(Qt::WA_ShowModal,true);
    this->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
    this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);

    m_sql.init("BatchDlg_data");

    QFile dlg_qss(":/qss/dlg.qss");
    if (dlg_qss.open(QFile::ReadOnly))
    {
        QTextStream filetext(&dlg_qss);
        QString stylesheet = filetext.readAll();
        this->setStyleSheet(stylesheet);
        dlg_qss.close();
    }
    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectC = new QGraphicsDropShadowEffect(this);
    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);

    shadow_effectC->setOffset(0, 0);
    shadow_effectC->setColor(QColor(150,150,150));
    shadow_effectC->setBlurRadius(6);


    ui->BatchCreate_gBox_1->setGraphicsEffect(shadow_effectA);
    ui->BatchCreate_gBox_2->setGraphicsEffect(shadow_effectB);
    ui->BatchCreate_gBox_3->setGraphicsEffect(shadow_effectC);
    ui->tableWidget->setAlternatingRowColors(true);

    ui->Box_Plan->setView(new  QListView());
    ui->Box_Customer->setView(new  QListView());


    m_pMsg = new Message(this);
    iniPage();
}

BatchDlg::~BatchDlg()
{
    delete ui;
}

void BatchDlg::iniPage()
{
    ui->Box_Plan->clear();
    ui->Box_Customer->clear();

    QList<T_PlanLine> plan_recoders = m_sql.queryAllPlanLines();
    foreach(T_PlanLine recoder,plan_recoders)
    {
        ui->Box_Plan->addItem(recoder.planName,recoder.id);
    }
}

void BatchDlg::on_Bt_Quite_clicked()
{
    this->close();
}


void BatchDlg::on_Bt_Save_clicked()
{
    QString strBatch;
    strBatch = ui->Edit_Batch->text();
    if(strBatch.isEmpty()){
        m_pMsg->Push(MESSAGE_TYPE_ERROR, tr("The batch name cannot be empty!"));
        return;
    }
    T_Batch m_data;
    m_data.batch_name = strBatch;
    m_data.plan_id = ui->Box_Plan->currentData().toInt();
    m_data.plan_name = ui->Box_Plan->currentText();
    m_data.user_id = ui->Box_Customer->currentData().toInt();
    m_data.user_name = ui->Box_Customer->currentText();

    if(!m_sql.insertBatch(m_data)){
        m_pMsg->Push(MESSAGE_TYPE_ERROR, tr("The batch insert failed!"));
        return;
    }

    emit sigFormInfo();
    this->close();
}

